package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.CuccContextCustDTO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;


@Service
public class IPAddressRecordInfoAttrHandler extends AbstractAttrHandler {
    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        String sceneInstId = KiteWebContext.getContext().getCurrentSceneInstId();
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        CuccContextCustDTO custDTO1 = (CuccContextCustDTO) KiteWebContext.getContext().getCust();
        if (LocalKeyConsts.PROVINCE_GUANGDONG.equals(sceneInst.getProvinceNbr())) {
            commonAttr.setIsEdit("F");
            commonAttr.setIsNull("F");
            commonAttr.setValue("T");
            setCommonAttr(commonAttr);
        } else {
            // FIXME: 订购之后的场景回填IP地址备案信息，订购的时候勾选了，则回填勾选，订购是否勾选状态判断(以该模块必填属性客户性质属性判断)
            List<KiteInst> prodInstList = querySceneInstService.listInsts(sceneInstId, Collections.singletonList(InstType.PROD_INST));
            if (KiteListUtils.isNotEmpty(prodInstList)) {
                prodInstList.forEach(prodInst ->{
                    KiteAttr prodAttr = querySceneInstService.getProdInstAttr(custDTO1.getCustId(), prodInst.getInstId(), "210004041");
                    if (Objects.nonNull(prodAttr) && KiteStringUtils.isNotEmpty(prodAttr.getValue())) {
                        commonAttr.setValue("T");
                        setCommonAttr(commonAttr);
                    }
                });
            }
        }
    }
}
